<?php
//******************
//*register sidebar*
//******************
?>
<?php 
if(function_exists(register_sidebar)) {
    register_sidebar(array('name' => __( 'Primary', 'kennyno3' ),
    'description'   => 'Placed into the single page',
    'id' => 'primary',
    'before_title' =>  '<h3 class="widget-title">',
    'after_title' => '</h3>',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    ));
    
    register_sidebar(array('name' => __( 'Secondary', 'kennyno3' ),
    'description'   => 'Placed into the footer',
    'id' => 'secondary',
    'before_title' =>  '<h3 class="widget-title">',
    'after_title' => '</h3>',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    ));
};
//add post thumbnails support into admin panel
add_theme_support( 'post-thumbnails' ); 

?>
<?php 

//***************
//*Menu supports*
//***************

//kiem tra su ton tai ham wp_nav_menu
if (function_exists('wp_nav_menu'))
{
  function wp_my_menus()
  {
    //dang ky menu cho theme
    register_nav_menus(array('menu-1' => __('First Menu')
                            ,'menu-2' => __('Second Menu')
                            ,'menu-3'=> __('Third Menu')));
  }

  //theme vao action
   add_action('init', 'wp_my_menus');
}

//Hien thi ra thong bao khi admin chua setup menu cho theme
function alert_menu()
{
echo '<div style="color: white;" >Go to Admin > Appearance > Menus to set up the menu. You need to run WP 3.0+ for custom menus to work.</div>';
}

?>
<?php 
//this function will be called in the next section
function trangexpress_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
 
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID();?>" class="clearfix">
    <div class="comment-author vcard">
        <?php echo get_avatar($comment,$size='90',$default='<path_to_url>' ); ?>
        <a href="<?php the_author_meta( 'user_url'); ?>" class="fn"><?php printf(__('%s'), get_comment_author_link()) ?></a>
        <p class="comment-meta">
            <small><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></small>
            <?php if ($comment->comment_approved == '0') : ?>
            <em><?php _e('Your comment is awaiting moderation.') ?></em>
            <br />
            <?php endif; ?>
        </p>
    </div>
        <div class="commentBody">
            <div class="comment-text">	
                <?php comment_text() ?>
            </div>
         
            <p class="reply">
              <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            </p>
        </div>
</div>
<?php } ?>
<?php

//**************
//*add meta box*
//**************


$prefix = '';
 
$meta_box = array(
    'id' => 'my-meta-box',
    'title' => "Product's Meta Data",
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => "Imagelink 1",
            'desc' => 'Hinh 1',
            'id' => $prefix . 'imagelink1',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => "Imagelink 2",
            'desc' => 'Hinh 2',
            'id' => $prefix . 'imagelink2',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => "Imagelink 3",
            'desc' => 'Hinh 3',
            'id' => $prefix . 'imagelink3',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => "Shop link",
            'desc' => 'Lien ket den shop',
            'id' => $prefix . 'shoplink',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Price before tax',
            'desc' => 'Gia tren webstore',
            'id' => $prefix . 'price',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Price after tax',
            'desc' => 'Gia ve VietNam',
            'id' => $prefix . 'pricevn',
            'type' => 'text',
            'std' => ''
        )
    ));

// Add meta box
function my_add_box() {
    global $meta_box;
    add_meta_box($meta_box['id'], $meta_box['title'], 'my_show_box', 'post', $meta_box['context'], $meta_box['priority']);
}

add_action('admin_menu', 'my_add_box');

function my_show_box() {
    global $meta_box, $post;
     
    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
     
    echo '<table class="form-table">';
 
    foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);
         
        echo '<tr>',
                '<th style="width:15%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', htmlspecialchars($meta) ? htmlspecialchars($meta) : htmlspecialchars($field['std']), '" size="30" style="width:60%" title="', $field['desc'],'" />', '<br />';
                break;
            case 'textarea':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" title="', $field['desc'], '" cols="100" rows="4" style="width:50%">', $meta ? $meta : $field['std'], '</textarea>', '<br />';
                break;
            case 'select':
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>';
                break;
            case 'radio':
                foreach ($field['options'] as $option) {
                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                }
                break;
            case 'checkbox':
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                break;
        }
        echo     '<td>',
            '</tr>';
    }
     
    echo '</table>';
}

// Save data from meta box
function izship_save_data($post_id) {
    global $meta_box;
     
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }
 
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
 
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }
     
    foreach ($meta_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
         
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}
add_action('save_post', 'izship_save_data');

?>
<?php

//*****************************
//*Setting theme page in admin*
//*****************************


//add setting theme menu into admin page
function setting_theme_admin_menu() {
    add_menu_page('Theme settings', 'Theme settings', 'manage_options', 'theme_settings', 'theme_settings_page'); 
}
add_action('admin_menu','setting_theme_admin_menu');

//add custom style into admin page
function my_admin_style() {
    wp_enqueue_style('my_style', get_template_directory_uri() . '/css/admin.css');
}
add_action('admin_print_styles','my_admin_style');
?>
<?php 

//Build theme setting page

function theme_settings_page() {
?>
    <div class="wrap my_wrap">  
    <div><?php screen_icon('themes'); ?> <h2 class="my_theme_h2">Setting Theme Page</h2></div><br />
    <div>
        <span style="font-size: 1.4em;"> Categories List</span> <span style="font-style: italic; margin-left: 10px;">(Which will be show at front page)</span>
    </div>
    <!--Show categories list-->
    <div>
    <form method="post" action="<?php get_template_directory_uri().'/functions.php'?>">
    <div style="position: relative;">
        <div class="my_cat_list my_border_radius_5 clearfix" >
            <h3 ><span>Categories</span></h3>
            <div class="my_list_cover my_border_radius_5">
                <p>
                    <ul>
                        <?php 
                            $args = array(
                                        'type'                     => 'post',
                                        'child_of'                 => 0,
                                        'parent'                   => '',
                                        'orderby'                  => 'name',
                                        'order'                    => 'ASC',
                                        'hide_empty'               => 0,
                                        'hierarchical'             => 1,
                                        'exclude'                  => '',
                                        'include'                  => '',
                                        'number'                   => '',
                                        'taxonomy'                 => 'category',
                                        'pad_counts'               => false );
                            $categories = get_categories($args);
                            foreach ($categories as $cat) {
                                echo '<li><input type="checkbox" value="'.$cat->cat_ID.'" name="cat_id[]"/>'.$cat->name.'</li>';
                            };                                        
                        ?>
                    </ul>              
                </p>
            </div>
            <p>
                <a class="select_all"><span>Select all</span></a>
                <input type="submit" class="button-secondary submit-add-to-menu right" value="Add to Front Page" name="add_cat_list_show" />
            </p>
        </div>
        <div class="my_cat_front_list my_border_radius_5 clearfix">
            <h3 ><span>Categories</span></h3>
            <div class="my_list_show_cover">
            <ul>
                <?php 
                    //Use the function update_option() to update a named option/value pair to the options database table
                    //http://codex.wordpress.org/Function_Reference/update_option
                    if(isset($_POST['add_cat_list_show'])) {
                        if($_POST['cat_id']) {
                            $ca_id_list = get_option('category_list_front_page');
                            foreach($_POST['cat_id'] as $id) {
                                $ca_id_list[] = $id;
                            }
                            update_option('category_list_front_page',$ca_id_list);
                            update_option('my_cat_list_update',true);
                        };
                    };
                    
                    if(isset($_POST['clear_option'])) {
                        update_option('category_list_front_page',array());
                        update_option('my_cat_list_update',true);
                    };
                    function show_cat_list() {
                        if(get_option('category_list_front_page')&&get_option('my_cat_list_update')) {
                            $cat_id_list = get_option('category_list_front_page');
                            foreach ($cat_id_list as $id) {
                                echo '<li id="shown_cat_id_'.$id.'" class="my_cat_list_front_page my_border_radius_5">'.get_cat_name($id).'</li>';
                            };
                        }else{
                            update_option('my_cat_list_update',false);
                        }
                    };
                    show_cat_list();
                ?>
            </ul>
            <p><input type="submit" name="clear_option" value="Delete All" class="button-secondary submit-add-to-menu right"/></p>
            </div>
            
        </div>
    </div>
    </form>
	</div>
<?php } ?>
